import { Injectable } from '@nestjs/common';
import { DiaryFindDao, DiaryFindByUserDao } from './interfaces/mine.interface';
import { InjectRepository } from '@nestjs/typeorm';
import { DiaryBill, Diary } from 'src/diary/diary.entity';
import { User } from 'src/user/user.entity';
import { VipInfo } from 'src/vipInfo/vipinfo.entity';
import { Repository } from 'typeorm';
import { VipInfoService } from 'src/vipInfo/vipInfo.service';

@Injectable()
export class MineService {
  constructor(
    @InjectRepository(DiaryBill)
    private diaryBillRepository: Repository<DiaryBill>,
    @InjectRepository(Diary)
    private diaryRepository: Repository<Diary>,
    @InjectRepository(User)
    private userRepository: Repository<User>,
    @InjectRepository(VipInfo)
    private vipInfoRepository: Repository<VipInfo>,
    private readonly vipInfoService: VipInfoService,

  ) { }

  findDiaryBill(userid: string) {
    return this.diaryBillRepository.find({ where: { userid: userid } });
  }

  findDiary(diaryFindDao: DiaryFindDao) {
    return this.diaryRepository.find({ where: { diarybillid: diaryFindDao.diarybillid } });
  }

  findDiaryByUser(diaryFindByUserDao: DiaryFindByUserDao) {
    console.log(diaryFindByUserDao);
    return this.diaryRepository.find({
      where: { userid: diaryFindByUserDao.userid },
      order: { createTime: 'DESC' },
      skip: diaryFindByUserDao.last,
      take: 5
    });
  }

  async findMineInfo(userid: string) {
    const user = await this.userRepository.findOne({ where: { userid: userid } });
    const isVip = await this.vipInfoService.isVipUser(userid)
    let combinedInfo = {
      ...user,
      isVip: isVip
    }
    if (!isVip) {
      return combinedInfo;
    } else {
      const vipInfo = await this.vipInfoRepository.findOne({ where: { userid: userid } })
      return {
        ...combinedInfo,
        vipStartTime: vipInfo.startTime,
        vipEndTime: vipInfo.endTime,
        vipIsPermanent: vipInfo.isPermanent
      };
    }
  }
}